package com.kc.zhaojiu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kc.zhaojiu.entity.TbCompany;
import com.kc.zhaojiu.mapper.TbCompanyMapper;
import com.kc.zhaojiu.service.TbCompanyService;
import com.kc.zhaojiu.service.ex.CompanyInfoNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 张琼珂
* @description 针对表【tb_company】的数据库操作Service实现
* @createDate 2022-10-22 21:36:24
*/
@Service
public class TbCompanyServiceImpl extends ServiceImpl<TbCompanyMapper, TbCompany>
    implements TbCompanyService{

    @Autowired
    private TbCompanyMapper tbCompanyMapper;

//    @Override
//    public TbCompany getCompanyInfo(long id) {
//
//        QueryWrapper<TbCompany> queryWrapper=new QueryWrapper();
//        queryWrapper.eq("id",id);
//        TbCompany res = tbCompanyMapper.selectOne(queryWrapper);
//        if(res==null){
//            throw new CompanyInfoNotFoundException("未找到该公司信息");
//        }
//
//        return res;
//    }

    @Override
    public List<TbCompany> getCompanyByTeaId(long t_id) {
        QueryWrapper<TbCompany> queryWrapper=new QueryWrapper();
        queryWrapper.eq("teacher_id",t_id);
        List<TbCompany> res = tbCompanyMapper.selectList(queryWrapper);
        return res;
    }

    @Override
    public List<TbCompany> getCompanyByName(String hrName) {
        QueryWrapper<TbCompany> queryWrapper=new QueryWrapper();
        queryWrapper.eq("hr_name", hrName);

        List<TbCompany> res = tbCompanyMapper.selectList(queryWrapper);

        return res;
    }

    @Override
    public Integer modCompanyInfo(TbCompany tbCompany) {
        Long id = tbCompany.getId();
        companyInfoIsNull(id,"查询不到该条信息");
        int i = tbCompanyMapper.updateById(tbCompany);
        System.out.println("i:"+i);

        return i;
    }

    @Override
    public Integer moveCompany(Long id) {
        companyInfoIsNull(id,"查询不到该条信息");

        int i = tbCompanyMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer addCompany(TbCompany tbCompany) {
        int i = tbCompanyMapper.insert(tbCompany);
        return i;
    }


    private void companyInfoIsNull(long id, String errorInfo) {
        QueryWrapper<TbCompany> queryWrapper=new QueryWrapper();
        queryWrapper.eq("id", id);
        TbCompany one = tbCompanyMapper.selectOne(queryWrapper);
        System.out.println("one:"+one);
        if(one==null){
            throw new CompanyInfoNotFoundException(errorInfo);
        }
    }


}




